package com.train.fibonacci.generate;

import com.train.fibonacci.report.FibonacciReporter;

public class RecursiveFibonacciGenerator implements FibonacciGenerator {

	@Override
	public void generate(int length, FibonacciReporter reporter) {
		if (length < 0) {
			throw new IllegalArgumentException();
		}
		
		for (int i = 0; i < length; i++) {
			long num = calculate(i);
			reporter.report(num);
		}
	}
	
	private long calculate(int index) {
		System.out.println("Log: calculate Fibonacci index: " + index);
		if (index == 0) {
			return A;
		} else if (index == 1) {
			return B;
		} else {
			return calculate(index -2) + calculate(index - 1);
		}
	}
}